/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sim;

/**
 *
 * @author Quest
 */
import java.io.*;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class VrmlParser {

    File file;
    String contentFile = "";
    public DefaultMutableTreeNode rootTree;

    public VrmlParser() {
    }

    public VrmlParser(String file) {
    }
    private boolean isVrmlFile = false;

    public boolean isVrmlFile() {
        return isVrmlFile;
    }

    private  String getFileContents(File file) {
        //StringBuilder contents = new StringBuilder();
        //ArrayList<String> nodes = new ArrayList<String>();
        String content = "";
        try {
            BufferedReader input = new BufferedReader(new FileReader(file));
            try {
                String line = null;
                int i = 0;
                while ((line = input.readLine()) != null) {
                    //System.out.println(i++ + " - " + line);
                    if (i == 0) {
                        if (line.startsWith("#VRML V1.0 ascii")) {
                            isVrmlFile = true;
                        } else {
                            isVrmlFile = false;
                        }
                    }
                    line = line.trim();
                    if ((!line.startsWith("#")) && (!line.equalsIgnoreCase(""))) {
                        //contents.append(line);
                        //contents.append(System.getProperty("line.separator"));
                        //content += contents.toString();
                        content += line + System.getProperty("line.separator");
                    }
                    i++;
                }

                content = content.trim();
                //content = content.

                if (content.endsWith("}")) {
                    isVrmlFile = isVrmlFile && Boolean.TRUE;
                } else {
                    isVrmlFile = isVrmlFile && Boolean.FALSE;
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        contentFile = content;
        return content;
    }


    private void parseString(String str_content) {
        String content = str_content;
        rootTree = new DefaultMutableTreeNode("Separator", true);
        int count = 1;
        int start = 0;
        int begin = content.indexOf("{", start);
        int end = content.lastIndexOf("}");
        //while (true) {
        //while (count < 4) {
        int open = content.indexOf("{", start);
        int first_next_open = content.indexOf("{", open + 1);
        //System.out.println("first = " + first_next_open);
        int second_next_open = content.indexOf("{", first_next_open + 1);
        //System.out.println("second = " + second_next_open);
        String new_node_string = content.substring(open + 1, first_next_open).trim();
        //DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_node_string);
        int rb = 1;
        int close;
        close = content.indexOf("}", open + 1);
        while (close < end) {
            DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(new_node_string);
            System.out.println("Open = " + open + " - Close = " + close + " - End = " + end);

            if (second_next_open > close) {
                int return_start = content.indexOf(System.getProperty("line.separator"), first_next_open);
                int return_mark = content.indexOf(System.getProperty("line.separator"), return_start + 1);
                int next_return_mark = content.indexOf(System.getProperty("line.separator"), return_mark + 1);
                
                System.out.println("rb = " + rb++);
                //do {
                while (true) {
                    new_node_string = content.substring(return_start + 1, return_mark).trim();
                    DefaultMutableTreeNode new_node_child = new DefaultMutableTreeNode(new_node_string);
                    System.out.println(new_node_string);
                    new_node.add(new_node_child);
                    return_start = return_mark;
                    //System.out.println("return_start = " + return_start);
                    return_mark = next_return_mark;
                    //System.out.println("return_mark = " + return_mark);
                    next_return_mark = content.indexOf(System.getProperty("line.separator"), return_mark + 2);
                    /*
                    System.out.println("next_return_mark = " + next_return_mark);
                    System.out.println("Count = " + count++);
                    System.out.println("Close = " + close);
                    System.out.println("Open = " + open);
                    System.out.println("return_mark = " + return_mark + " - " + "Close = " + close);
                    */
                    count++;
                    if (return_mark > close) {
                        break;
                    }
                } //while (return_mark < close);

                
                /*int return_mark = content.indexOf(System.getProperty("line.separator"), first_next_open + 2);
                System.out.println("First Return mark = " + return_mark);
                int next_return_mark = content.indexOf(System.getProperty("line.separator"), return_mark + 1);
                System.out.println("Second Return mark  = " + next_return_mark);
                System.out.println("Close  = " + close);
                do {
                    new_node_string = content.substring(first_next_open + 1, return_mark).trim();
                    DefaultMutableTreeNode new_node_child = new DefaultMutableTreeNode(new_node_string);
                    //System.out.println(new_node_string);
                    new_node.add(new_node_child);
                    first_next_open = return_mark;
                    //System.out.println("First = " + first_next_open);
                    return_mark = next_return_mark;
                    //System.out.println("Return = " + return_mark);
                    next_return_mark = content.indexOf(System.getProperty("line.separator"), return_mark + 1);
                //System.out.println("Second Return = " + next_return_mark);
                } while (return_mark < close);
                */
            } else {
                //do {
                while (true) {
                    new_node_string = content.substring(first_next_open + 1, second_next_open).trim();
                    DefaultMutableTreeNode new_node_child = new DefaultMutableTreeNode(new_node_string);
                    System.out.println("New node = " + new_node_string);

                    int return_mark = content.indexOf(System.getProperty("line.separator"), second_next_open + 2);
                    int next_return_mark = content.indexOf(System.getProperty("line.separator"), return_mark + 1);

                    first_next_open = second_next_open;
                    do {
                        new_node_string = content.substring(first_next_open + 1, return_mark).trim();
                        DefaultMutableTreeNode new_node_child_of_child = new DefaultMutableTreeNode(new_node_string);
                        System.out.println("New node child = " + new_node_string);
                        new_node_child.add(new_node_child_of_child);
                        //new_node.add(new_node_child);
                        first_next_open = return_mark;
                        System.out.println("First = " + first_next_open);
                        return_mark = next_return_mark;
                        System.out.println("Return = " + return_mark);
                        next_return_mark = content.indexOf(System.getProperty("line.separator"), return_mark + 1);
                        System.out.println("Second Return = " + next_return_mark);
                        System.out.println("Count = " + count++);
                    } while (return_mark < close);
                    new_node.add(new_node_child);

                    int next_open = content.indexOf("{", close + 1);
                    int next_close = content.indexOf("}", close + 1);
                    if (next_open < next_close) {
                        open = close;
//                        first_next_open = next_open;
//                        second_next_open = content.indexOf("{", first_next_open + 1);
//                        close = next_close;

                        //first_next_open = close;
                        //second_next_open = next_open;
                        //close = next_close;
                        close = content.indexOf("}", open + 1);

                        continue;
                    } else {
                        open = close;
                        //first_next_open = close;
                        //second_next_open = next_open;
                        //close = next_close;
                        close = content.indexOf("}", open + 1);
                        break;
                    }
                } //while (true);
            }
            
            if (count > 100){
                //break;
            }
            rootTree.add(new_node);
            open = close;
            close = content.indexOf("}", open + 1);
            first_next_open = content.indexOf("{", open + 1);
            second_next_open = content.indexOf("{", first_next_open + 1);
            new_node_string = content.substring(open + 1, first_next_open).trim();
            new_node = new DefaultMutableTreeNode(new_node_string);
        }
    }

    private ArrayList<String> getNodes() {
        return null;
    }

    public static void main(String[] aArguments) throws IOException {
        VrmlParser vrml = new VrmlParser();
        File file = new File("C:\\tiep.wrl");
        String a = vrml.getFileContents(file);
        vrml.parseString(a);
        //System.out.println(a);
        System.out.println(vrml.isVrmlFile());



        JFrame frame = new JFrame("Creating a JTree Component!");
    
//    DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Color", true);
//    DefaultMutableTreeNode black = new DefaultMutableTreeNode("Black");
//    DefaultMutableTreeNode blue = new DefaultMutableTreeNode("Blue");
//    DefaultMutableTreeNode nBlue = new DefaultMutableTreeNode("Navy Blue");
//    DefaultMutableTreeNode dBlue = new DefaultMutableTreeNode("Dark Blue");
//    DefaultMutableTreeNode green = new DefaultMutableTreeNode("Green");
//    DefaultMutableTreeNode white = new DefaultMutableTreeNode("White");
//    parent.add(black);
//    parent.add(blue);
//    blue.add(nBlue);
//    blue.add(dBlue);
//    parent.add(green );
//    parent.add(white);
//
//    JTree tree = new JTree(parent);
    
    JTree tree = new JTree(vrml.rootTree);
    //System.out.println("childcount = " + vrml.rootTree.getChildAt(0).getChildAt(0).getChildAt(0).getChildAt(0).getChildCount());
    //System.out.println("Count = " + tree.getRowCount());
    tree.setRootVisible(true);
    //System.out.println(tree.getVisibleRowCount() + " - " + tree.getRowHeight());
    frame.add(tree);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    frame.setSize(200,200);
    frame.setVisible(true);
    }
}
